module.exports = {
	/**
	 * 提现管理
	 * @url admin/system/role/sys/update 前端调用的url参数地址
	 * data 请求参数 说明
	 * @param {String} user_id
	 * @param {Boolean} price
	 * res 返回参数说明
	 * @param {Number} code 错误码，0表示成功
	 * @param {String} msg 详细信息
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: '' };
		// 业务逻辑开始----------------------------------------------------------- 
		let plaform = ''
		if(originalParam.context.PLATFORM === 'mp-weixin'){
			plaform = 'wx'
		}
		let {
			user_id,
			price
		} = data;
		// 创建回滚
		const transaction = await vk.baseDao.startTransaction();
		try {
			let bill_cost = 0
			//查询手续费
			let client = await vk.baseDao.findByWhereJson({
				dbName: 'bus-wx-config',
				whereJson: {
					config_key: plaform+'_config'
				}
			})
			if(client.bill_cost) bill_cost = client.bill_cost
			
			// 执行数据库API请求
			var cash_num = await vk.baseDao.count({
				dbName: 'bus-wx-auth-cash',
				whereJson: {
					user_id,
					status: 0
				}
			})
			if(cash_num > 0){
				return {
					code: -1,
					msg:"已有提现审核中..."
				}
			}
			
			// 用户扣除余额
			if(isNaN(price) && parseInt(price) <= 0){
				return {
					code: -1,
					msg: "金额输入错误"
				}
			}else{
				price = parseInt(price)
			}
			
			// 执行数据库API请求
			var auth = await vk.baseDao.findByWhereJson({
				dbName: 'bus-wx-auth',
				whereJson: {
					user_id
				}
			})
			if(!auth || !auth.alipay){
				return {
					code: -1,
					msg:"提现账户异常，请联系客服"
				}
			}
			
			let userInfo = await vk.baseDao.findByWhereJson({
				dbName:"uni-id-users",
				whereJson:{
					_id: user_id,
					price: _.gte(price)
				}
			})
			if(!userInfo){
				return {
					code: -1,
					msg:"余额不足"
				}
			}
			if(userInfo.status !== 0){
				return {
					code: -1,
					msg:"账户异常，请联系客服"
				}
			}
			let newPrice = Math.floor(userInfo.price * 100 - price * 100) / 100
			let reUserInfo = await vk.baseDao.updateById({
				dbName:"uni-id-users",
				id: user_id,
				dataJson: {
					price: newPrice
				},
				getUpdateData:true,
			});
			if(!reUserInfo){
				await transaction.rollback();
				return {
					code: -1,
					msg:"更新收益错误"
				}
			}
			
			var cash_id = await vk.baseDao.add({
				db:transaction,
				dbName: "bus-wx-auth-cash",
				dataJson: {
					user_id,
					name: auth.name,
					alipay: auth.alipay,
					before: userInfo.price,
					after: reUserInfo.price,
					price: price,
					reality: (1 - bill_cost) * price,
					fee: bill_cost,
					comment: '收益提现',
					status: 0
				}
			});
			if(!cash_id){
				await transaction.rollback();
				return {
					code: -1,
					msg:"提现记录添加异常"
				}
			}
			// 提交事物
			await transaction.commit();
			res.msg = "提交成功，等待审核"
		}catch (err) {
			// 事务回滚
			await transaction.rollback();
			console.error(`transaction error`, err)
			return {
				code: -1,
				msg:"数据库写入异常,事务已回滚",
				err: {
					message:err.message,
					stack:err.stack
				}
			}
		}
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}

}